/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fyx.web;

import cz.makrobioklub.action.web.ActionObserver;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Observer;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ApplicationScoped;
import org.fyx.cms.web.CmsObserver;
import org.fyx.linker.web.LinkerObserver;
import org.fyx.mula.web.MulaObserver;
import org.fyx.secure.web.SecureObserver;
import org.fyx.web.context.FyxContextWrapper;
import org.fyx.web.context.WebContext;

/**
 * Hlavni aplikacni beana
 * Zde jsou naplneny veskere systemove udalosti.
 * @author xljan
 */
@ManagedBean(eager = true)
@ApplicationScoped
public class ApplicationBean {
    /**
     * Kontejner s globalnimy observery.
     */
    private static List<Observer> globalObservers = new LinkedList<Observer>();
    @EJB
    private MulaObserver mulaObserver;
    @EJB
    private SecureObserver secureObserver;
    @EJB
    private LinkerObserver linkerObserver;
    @EJB
    private CmsObserver cmsObserver;
    @EJB
    private ActionObserver actionObserver;
    @EJB
    private FyxObserver fyxObserver;

    /** Creates a new instance of ApplicationBean */
    public ApplicationBean() {
        FyxContextWrapper.construct(new WebContext());
        java.util.Locale.setDefault(new Locale("cs", "CZ"));
    }

    @PostConstruct
    public void postConstruct() {
        initObservers();
        for(Observer observer : globalObservers){
            WebContext.getCurrentContext().getMenuCreatingEventHandler().
                addObserver(observer);
            WebContext.getCurrentContext().getRoleCreatingEventHandler().
                addObserver(observer);
        }
        WebContext.getCurrentContext().getRoleCreatingEventHandler().notifyObservers();
    }
    
    private void initObservers(){
        addGlobalObserver(mulaObserver);
        addGlobalObserver(secureObserver);
        addGlobalObserver(linkerObserver);
        addGlobalObserver(cmsObserver);
        addGlobalObserver(actionObserver);
        addGlobalObserver(fyxObserver);
    }
    
    /**
     * Prida observer, ktery se bude volat pri vsech udalostech v systemu.
     * 
     * @param observer 
     */
    public static void addGlobalObserver(Observer observer){
        globalObservers.add(observer);
    }

    /**
     * Vrati seznam vsech globalnich observeru.
     * @return 
     */
    public static List<Observer> getGlobalObservers() {
        return globalObservers;
    }
}
